package com.crazy.web.controller.share;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crazy.enums.Constants;
import com.crazy.model.major.dto.UserSession;
import com.crazy.model.major.entity.TLabel;
import com.crazy.rest.RestResult;
import com.crazy.web.controller.BaseController;
import com.crazy.web.service.share.LabelService;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.ValidationException;
import java.util.List;

/**
 * 标签的相关接口
 * @author zhaochaofeng
 * @version 1.0
 * @date 2020/11/24 10:37
 */
@Log4j2
@Controller
@RequestMapping("/share/label")
public class LabelController extends BaseController {

    @Autowired
    private LabelService labelService;

    @RequiresPermissions("label:view")
    @GetMapping("/view")
    public String view() {
        request.setAttribute("url", "/share/label/view");
        return "label/view";
    }

    @RequiresPermissions("label:add")
    @PostMapping("/add")
    @ResponseBody
    public RestResult add(@RequestParam String labelName, @RequestParam String alias, @RequestParam String introduction) {
        UserSession userSession = getUserSession();
        TLabel tLabel = new TLabel();
        tLabel.setLabelName(labelName);
        tLabel.setAlias(alias);
        tLabel.setIntroduction(introduction);
        labelService.add(userSession.getId(), tLabel);
        return RestResult.createSuccessResult("添加成功");
    }

    @RequiresPermissions("label:update")
    @PostMapping("/update")
    @ResponseBody
    public RestResult update() {
        return null;
    }

    @RequiresPermissions("label:delete")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult delete(@RequestParam("ids[]") long[] ids) {
        UserSession userSession = getUserSession();
        labelService.delete(userSession.getId(), ids);
        return RestResult.createSuccessResult("删除成功");
    }

    @RequiresPermissions("label:select")
    @GetMapping("/select")
    @ResponseBody
    public RestResult select(@RequestParam int page, @RequestParam int rows, @RequestParam(required = false) String search) {
        UserSession userSession = getUserSession();
        IPage<TLabel> labelIPage = labelService.find(userSession.getId(), page, rows, search);
        return RestResult.createSuccessResult("查询成功", labelIPage);
    }

    @GetMapping("/findAll")
    @ResponseBody
    public RestResult findAll() {
        UserSession userSession = getUserSession();
        List<String> labelNames = labelService.findAllByUserId(userSession.getId());
        return RestResult.createSuccessResult("查询成功", labelNames);
    }
}
